{% extends "base.html" %}
{% load event_tags %}
{% load display_tags %}
{% load static %}
{% block add_css %}
    {{ block.super }}
    <link rel="stylesheet" href="{% static "easymde/dist/easymde.min.css" %}">
{% endblock %}
{% block add_styles %}
    {{ block.super }}
    .chosen-container {
    width: 70% !important;
    }
    .editor-toolbar, .editor-statusbar, .editor-preview-side, .CodeMirror {
    width: 70% !important;
    }
{% endblock %}
{% block content %}
    {{ block.super }}
    <h3> Edit a Finding </h3>
    {% if temp %}
        <form id="add_finding" class="form-horizontal" action="{% url 'edit_finding' finding.id %}" method="post">
            {% csrf_token %}
            {% include "dojo/form_fields.html" with form=form %}
            {% if jform %}
                <h4> JIRA </h4>
                <hr>
                {% include "dojo/form_fields.html" with form=jform %}
            {% endif %}
            {% if gform %}
                <h3> GitHub </h3>
                <hr>
                {% include "dojo/form_fields.html" with form=gform %}
            {% endif %}
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <input type="submit" value="Add Another Finding"/>
                    <input type="submit" value="Finished"/>
                </div>
            </div>
        </form>
    {% else %}
        <form id="add_finding" class="form-horizontal" action="{% url 'edit_finding' finding.id %}" method="post">
            {% csrf_token %}
            {% if return_url %}
                <input type="hidden" name="return_url" value="{{ return_url }}" />
            {% endif %}
            {% include "dojo/form_fields.html" with form=form %}
            {% block additional_forms %}
            {% endblock additional_forms %}
            <span id="original_finding" class="small">
                {% if finding.duplicate_finding %}
                    [original:
                    <a href="{% url 'view_finding' finding.duplicate_finding.id %}">{{ finding.duplicate_finding.id }} : {{ finding.duplicate_finding.title }}</a>/
                    <a href="{% url 'view_product' finding.duplicate_finding.test.engagement.product.id %}">{{ finding.duplicate_finding.test.engagement.product.name }}</a>/
                    <a href="{% url 'view_engagement' finding.duplicate_finding.test.engagement.id %}">{{ finding.duplicate_finding.test.engagement.name }}</a>
                    {% if finding.duplicate_finding.test.title %}
                        {{finding.duplicate_finding.test.title}}
                    {% else %}
                        {{finding.duplicate_finding.test.test_type}}
                    {% endif %}
                    {% if finding.duplicate_finding.cve %}
                        {% if finding.duplicate_finding.cve|has_vulnerability_url %}
                            <a href="{{finding.duplicate_finding.cve|vulnerability_url}}">({{finding.duplicate_finding.cve}})</a>
                        {% else %}
                            {{ finding.duplicate_finding.cve }}
                        {% endif %}
                    {% endif %}
                    {% if finding.duplicate_finding.cwe > 0 %}
                        <a href="{{finding.duplicate_finding.cwe|cwe_url}}">(CWE-{{finding.duplicate_finding.cwe}})</a>
                    {% endif %}
                    {% if finding.duplicate_finding.jira_issue %}
                        <a href="{{ finding.duplicate_finding | jira_issue_url }}"
                        target="_blank" title="{{ finding.duplicate_finding | jira_issue_url }}">{{ finding.duplicate_finding | jira_key }}</a>
                    {% endif %}
                    ]
                {% endif %}
            </span>

            {% if jform %}
                <h4> JIRA </h4>
                <hr>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="id_jira_issue">JIRA URL
                        <i class="fa-solid fa-circle-question has-popover" data-trigger="hover" data-content="JIRA URL connected to this findings product or engagement" data-placement="right" data-container="body" data-original-title="" title="">
                        </i>
                    </label>
                    <div class="col-sm-10 form-control-static">
                        {% if finding.has_jira_group_issue %}
                            <a href="{{ finding.finding_group | jira_issue_url }}"
                            target="_blank" title="{{ finding.finding_group | jira_issue_url }} (group)">{{ finding.finding_group | jira_issue_url }} (group)</a>
                        {% elif finding.has_jira_issue %}
                            <a href="{{ finding | jira_issue_url }}"
                            target="_blank"> {{ finding | jira_issue_url }} </a>
                        {% else %}
                            <a href="{{ finding | jira_project_url }}"
                            target="_blank"> {{ finding | jira_project_url }} </a>
                        {% endif %}
                    </div>
                </div>
                {% include "dojo/form_fields.html" with form=jform %}
            {% endif %}
            {% if gform %}
                <h4> GitHub </h4>
                <hr>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="id_github_issue">GitHub issue
                        <i class="fa-solid fa-circle-question has-popover" data-trigger="hover" data-content="Github issue connected to this finding" data-placement="right" data-container="body" data-original-title="" title="">
                        </i>
                    </label>
                    <div class="col-sm-10 form-control-static">
                        {% if finding.github_issue and finding.github_conf_new %}
                            <a href="{{ finding.github_issue.issue_url }}" target="_blank" title="{{ finding.github_issue.issue_url }}">{{finding.github_issue.issue_url}}</a>
                        {% else %}
                            None
                        {% endif %}
                    </div>
                </div>
                 {% include "dojo/form_fields.html" with form=gform %}
            {% endif %}
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <input class="btn btn-primary" name="_Finished" type="submit" value="Finished"/>
                </div>
            </div>
        </form>
    {% endif %}

{% endblock %}
{% block postscript %}
    {{ block.super }}
    <script type="application/javascript" src="{% static "jquery.hotkeys/jquery.hotkeys.js" %}"></script>
    <script type="application/javascript" src="{% static "easymde/dist/easymde.min.js" %}"></script>
    <script type="text/javascript" src="{% static "admin/js/jquery.init.js" %}"></script>
    <script type="application/javascript" src="{% static "admin/js/admin/RelatedObjectLookups.js" %}"></script>
    <script type="application/javascript">
        $ = django.jQuery;
        $.hotkeys.options.filterInputAcceptingElements = false;
        $.hotkeys.options.filterTextInputs = false;
		
        var tmp_val = null;
		
        if ($('#id_mitigated').val() != null) {
            $('#id_mitigated').val($('#id_mitigated').val().slice(0,10));
            var tmp_val = $('#id_mitigated').val();
        }
		
        function displayMitigated() {
            var elMitigated = $('#id_mitigated').closest('.form-group'),
                elMitigatedBy = $('#id_mitigated_by').closest('.form-group'),
                elActive = $('#id_active')[0];

            if (!elActive.checked) {
                elMitigated.show('fast');
				$('#id_mitigated').val(tmp_val);
                elMitigatedBy.show('fast');
            } else {
                elMitigated.hide('fast');
				$('#id_mitigated').val('');
                elMitigatedBy.hide('fast');
            }
        }

        $(function () {
            $('[data-toggle="tooltip"]').tooltip()

            $(document).bind('keydown', 'ctrl+s', function (event) {
                if (event.preventDefault) {
                    event.preventDefault();
                } else {
                    // internet explorer
                    event.returnValue = false;
                }
                $("form#add_finding").submit();
            });

            $("textarea").each(function (index, elem) {
                if (elem.hasAttribute("required")) {
                    elem.removeAttribute("required");
                    elem.id = "req"
                }

                if (elem.name != 'endpoints_to_add' && elem.name != 'vulnerability_ids' && !$(elem).hasClass('select2-search__field')) {
                    var mde = new EasyMDE({
                        spellChecker: false,
                        inputStyle: "contenteditable",
                        element: elem,
                        autofocus: false,
                        forceSync: true,
                        toolbar: ["bold", "italic", "heading", "|",
                            "quote", "unordered-list", "ordered-list", "|",
                            "link", "image", "|",
                            "table", "horizontal-rule", "code", "|",
                            "guide"
                        ]
                    });
                    mde.render();
                }
            });

            displayMitigated();
            $('#id_active').change(displayMitigated);
        });

        {% comment %} crazy legacy stuff to force the original finding to be displayede at the right place {% endcomment %}
        window.onload = function() {
            var original_finding = document.getElementById("original_finding")
            var duplicate_checkbox = document.getElementById("id_duplicate").parentElement.parentElement
            if ($("#id_duplicate").prop("checked")) {
                $("#id_duplicate").parent().parent().append(original_finding)
            } else {
                $("#id_duplicate").click(function(){ alert('findings can only be marked as duplicates from the view finding screen. Similar Findings must be enabled for this operation.'); return false; });
            }
        };

        $("#add_finding").submit(function () {
            var isFormValid = true;

            $("textarea#req").each(function () {
                if ($.trim($(this).val()).length == 0) {
                    $(this).addClass("highlight");
                    isFormValid = false;
                }
                else {
                    $(this).removeClass("highlight");
                }
            });

            if (!isFormValid) alert("Please fill in all the required fields (indicated by *)");

            return isFormValid;
        });
    </script>

{% endblock %}
